﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.SqlServerDAL
{
    public class CurrencyDAL : ICurrencyDAL
    {
        #region 字段、构造方法、数据映射

        private Builder<Currency> builder = new Builder<Currency>("Currency", "Id");

        public CurrencyDAL(){
            this.builder.CreateModel += new ReaderBind<Currency>(builder_CreateModel);
            this.builder.GetParameters += new ParametersBuilder<Currency>(builder_GetParameters);
        }

        IDataParameter[] builder_GetParameters(Currency t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@Name",t.Name),
                DataAccess.CreateParameter("@Symbol",t.Symbol),
                DataAccess.CreateParameter("@Code",t.Code),
                DataAccess.CreateParameter("@Icon",t.Icon),
                DataAccess.CreateParameter("@IsDefault",t.IsDefault),
                DataAccess.CreateParameter("@ExchangeRate",t.ExchangeRate)};
        }

        Currency builder_CreateModel(Hashtable reader)
        {
            Currency model = new Currency();

            model.Id = reader.Get<int>("Id");
            model.Name = reader.Get<string>("Name");
            model.Symbol = reader.Get<string>("Symbol");
            model.Code = reader.Get<string>("Code");
            model.IsDefault = reader.Get<bool>("IsDefault");
            model.Icon = reader.Get<string>("Icon");
            model.ExchangeRate = reader.Get<decimal>("ExchangeRate");

            return model;
        }

        #endregion

        #region IExchnageReate

        public Currency FindById(int id)
        {
            return this.builder.GetModel(id);
        }

        public Currency FindByDefault()
        {
            return this.builder.GetModel(
                "IsDefault=1",
                string.Empty,
                null);
        }

        public IList<Currency> FindAll()
        {
            int total;
            return this.builder.GetModels(string.Empty, string.Empty, "order by Id asc", 1, 9999, out total, null);
        }

        public void Add(Currency currency)
        {
            if (currency.IsDefault)
                RemoveDefault();

            this.builder.Insert(
               currency,
               "Name",
               "Symbol",
               "Code",
               "IsDefault",
               "Icon",
               "ExchangeRate");
        }

        public void Update(Currency currency)
        {
            if (currency.IsDefault)
                RemoveDefault();

            this.builder.Update(
                currency,
                "Id=@Id",
                "Name",
                "Symbol",
                "Code",
                "IsDefault",
                "Icon",
                "ExchangeRate");
        }

        public void Delete(int id)
        {
            this.builder.Delete(id);
        }

        #endregion

        private void RemoveDefault()
        {
            this.builder.UpdateFields(
                "IsDefault",
                "IsDefault=1",
                DataAccess.CreateParameter("@IsDefault", false));

        }
    }
}
